package ChooseMethodView;

use strict;

use CGI qw(:standard);
use Athena::WebAPI::HTML;
use Athena::WebAPI::Form;
use Athena::WebAPI::Table;
use Athena::WebAPI::View;

use Athena::NavHeaderView;

use base ("View");

sub new {

    # Get the class name
    my $stInvocant = shift(@_);
    my $stClass = ref($stInvocant) || $stInvocant;

    # Create the current instance and bless into class
    my $rSelf = { };
    bless($rSelf,$stClass);

    my %hParams = ( @_ );
    $rSelf->{params} = \%hParams;

    return $rSelf;
}

sub draw {
  my $rSelf = shift(@_);

  my $rTable = Table->new(%{$rSelf->{params}->{table_style}});

  $rSelf->buildTable($rTable);

  my $rForm = Form->new(-method=>'post', -name=>'myForm');
  $rForm->addChild($rTable);
  $rForm->addHiddenFields($rSelf->{params}->{'hidden_fields'});

  my $rHTML = HTML->new("athena web view");
  $rHTML->setBodyAttr(%{$rSelf->{params}->{body_style}});
  $rHTML->addChild($rForm);
  
  return $rHTML->draw();
}

sub buildTable {
   my $rSelf = shift(@_);
   my $rTable = shift(@_);
   my $iCurRow = 0;
   $rTable->addChild($iCurRow++, 0, NavHeaderView->new($rSelf->{params}));
   $rTable->addChild($iCurRow++, 0, "Set LB Location",
		     -bgcolor=>'#ddcccc');

   my $rInnerTable = Table->new();
   $rInnerTable->addChild($iCurRow, 0, radio_group(-name=>'engine_method',
					    -value=>'http'));
   $rInnerTable->addChild($iCurRow++, 1, 
			  textfield(-name=>'engine_url',
				    -size=>'20',
				    -maxlength=>'128')
			  . "&nbsp;" .  b("remote mod_athena base url") 
			 . " eg. http://localhost/ath");

   $rInnerTable->addChild($iCurRow, 0, " ", -align=>'right');

   if ($rSelf->{params}->{url_history} ne "0") {
     $rInnerTable->addChild($iCurRow, 1, 
			    popup_menu(-name=>'engine_url_history',
				       -values=>$rSelf->{params}->{url_history},
				       -onClick=>"document.myForm.engine_url.value = document.myForm.engine_url_history.value; return false"
				      ) . " url history");
   }
   else {
     $rInnerTable->addChild($iCurRow, 1, "(empty)");
   }
   $iCurRow++;

#   $rInnerTable->addChild($iCurRow, 0, radio_group(-name=>'engine_method',
#					    -value=>'athena',
#					    -default=>'-'));
#   $rInnerTable->addChild($iCurRow++, 1,
#			  textfield(-name=>'engine_host',
#				    -size=>'12',
#				    -maxlength=>'128')
#			  . "&nbsp;" . b("athena daemon host") . "&nbsp;" 
#			  . textfield(-name=>'engine_port',
#				      -size=>'5',
#				      -maxlength=>'8')
#			  . "&nbsp;" . b("port"));
   $rInnerTable->addChild($iCurRow++, 0, 
			  "<br>Settings are stored in cookie.<br> Current settings:<br><ul>"
			  . "<li> Method: " 
			  . $rSelf->{params}->{ck_engine_method}
			  . "<li> Url: "
			  . $rSelf->{params}->{ck_engine_url}
			  . "<li> Host: "
			  . $rSelf->{params}->{ck_engine_host}
			  . "<li> Port: "
			  . $rSelf->{params}->{ck_engine_port}
			  . "</ul>"
			  . "Note that since the settings are in your cookies, you will need to actually hit send TWICE to see it show up here, although it is set after the first submit.",
			  -colspan=>2);
   $rTable->addChild($iCurRow++, 0, $rInnerTable);
   $rTable->addChild($iCurRow++, 0, submit(-name=>'send'), 
		     -bgcolor=>'#ddcccc');
 }

1;
